'''
Created on 22/03/2011

@author: Renzo Nuccitelli
'''
from google.appengine.ext import db
from ce.convertion.field import FieldsDescriptor, MultipleStringField
from ce.convertion.html import Builder, TableColumn
import unittest

class RequestStub(object):
    def get(self, prop):
        return {"email":"m1@blah.com", "email2":"m2@blah.com", "foo":"foo"}.get(prop)
    
    def get_all(self,prop):
        return ["choice1","choice2"]


class ModelStub(db.Model):
    email = db.EmailProperty()
    email2 = db.EmailProperty(required = True)
    choices=db.StringListProperty()


class FieldsDescriptorStub(FieldsDescriptor):
    def __init__(self):
        super(FieldsDescriptorStub, self).__init__(ModelStub)

    def fields(self):
        return [{"email":"Email"}, { "email2":"Email"},{"choices":MultipleStringField("Escolhas",["Escolha 1","Escolha 2"],choices=["choice1","choice2"])}]

class FormStub(Builder):
    def __init__(self):
        super(FormStub, self).__init__(FieldsDescriptorStub())



class FormTests(unittest.TestCase):
    def testGetHtmlInputs(self):
        form = FormStub()
        print form.getHtmlInputs("Teste", {"email":"m1@blah.com", "email2":"m2@blah.com"}, {"email":"Chora"})
        print "Outro Builder\n\n\n"
        print form.getHtmlInputs("Teste", ModelStub(email = "m1@model.com", email2 = "m2@model.com"))
        print "Outro Builder\n\n\n"

    def testGetFullForm(self):
        items = [ModelStub(email = "m1@model.com", email2 = "m2@model.com"), ModelStub(email = "m1@model2.com", email2 = "m2@model2.com"), ModelStub(email = "m1@model3.com", email2 = "m2@model3.com")]
        for i in items:
            i.con = i.email + " e " + i.email2
        columns = [TableColumn("con", "Concatenado")]
        form = FormStub()
        print form.buildTable(items, columns)

    def testBuildTable(self):
        form = FormStub()

    def testBuildModelDict(self):
        form = FormStub()
        d = form.buildModelDict(RequestStub())
        self.assertEqual({"email":"m1@blah.com", "email2":"m2@blah.com","choices":["choice1","choice2"]}, d)

    def testSetModelFromRequest(self):
        form = FormStub()
        model = ModelStub(email = "m@blah.com", email2 = "m@blah.com",choices=["choice1","choice2"])
        form.setModelFromRequest(model, RequestStub())
        self.assertEqual(model.email, "m1@blah.com")
        self.assertEqual(model.email2, "m2@blah.com")
        self.assertEqual(model.choices, ["choice1","choice2"])


if __name__ == "__main__":
    unittest.main()

